/* Copyright (c) 2010 OFXKit
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import "OFXStatementRequest.h"


@implementation OFXStatementRequest

@synthesize account;
@synthesize accountPrefix;
@synthesize dateStart;
@synthesize dateEnd;
@synthesize includeTransactions;
@synthesize includeTransactionImageInformation;

- (id)init
{
  self = [super init];
  self.accountPrefix = @"BANK";
  return self;
}

- (NSString*)formRequestForOFXVersion:(NSUInteger)ofxVersion
{
  if (ofxVersion >= 100 && ofxVersion <= 106) {
    return [self formOFX1Request];
  } else if (ofxVersion >= 200 && ofxVersion <= 211) {
    return [self formOFX2Request];
  }
  
  // unsupported version of ofx requested
  return nil;
}

- (NSString*)formOFX1Request
{
  // in this instance the OFX 1 and 2 requests are nearly identical.
  // only difference being that OFX 1 does not support
  // includeTransactionImageInformation.  Get the OFX2 request and just delete
  // this element.
  NSString* result = [self formOFX2Request];
  NSRange start = [result rangeOfString:@"<INCTRANIMG>"];
  NSRange end = [result rangeOfString:@"</INCTRANIMG>"];
  NSUInteger beginIdx = start.location;
  NSUInteger endIdx = end.location + end.length;
  NSString* firstHalf = [result substringToIndex:beginIdx];
  NSString* secondHalf = [result substringFromIndex:endIdx];
  return [NSString stringWithFormat:@"%@%@", firstHalf, secondHalf];
}

- (NSString*)formOFX2Request
{
  NSMutableString* result = [NSMutableString stringWithCapacity:1000];
  [result appendFormat:@"<%@STMTRQ>", accountPrefix];
  
  if (account != nil) {
    [result appendFormat:@"<%@ACCTFROM><ACCTID>%@</ACCTID></%@ACCTFROM>",
     accountPrefix, [account accountId], accountPrefix];
  } else {
    [result appendFormat:@"<%@ACCTFROM><ACCTID>NULL</ACCTID></%@ACCTFROM>",
     accountPrefix, accountPrefix];
  }
  
  [result appendString:@"<INCTRAN>"];
  if (dateStart != nil) {
    [result appendFormat:@"<DTSTART>%@</DTSTART>", 
     [defaultOFXDateFormat stringFromDate:dateStart]];
  }
  
  if (dateEnd != nil) {
    [result appendFormat:@"<DTEND>%@</DTEND>",
     [defaultOFXDateFormat stringFromDate:dateEnd]];
  }
  
  if (includeTransactions) {
    [result appendString:@"<INCLUDE>Y</INCLUDE>"];
  } else {
    [result appendString:@"<INCLUDE>N</INCLUDE>"];
  }
  [result appendString:@"</INCTRAN>"];
  
  if (includeTransactionImageInformation) {
    [result appendString:@"<INCTRANIMG>Y</INCTRANIMG>"];
  } else {
    [result appendString:@"<INCTRANIMG>N</INCTRANIMG>"];
  }

  [result appendFormat:@"</%@STMTRQ>", accountPrefix];
  return result;
}

@end
